package com.github.niefy.modules.wx.config;
import com.github.niefy.modules.wx.handler.*;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static me.chanjar.weixin.common.api.WxConsts.EventType;
import static me.chanjar.weixin.common.api.WxConsts.EventType.*;
import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType.EVENT;
import static me.chanjar.weixin.mp.constant.WxMpEventConstants.CustomerService.KF_CREATE_SESSION;
@RequiredArgsConstructor
@Configuration
public class WxMpMessageRouterConfiguration {
  private final LogHandler logHandler;
  private final NullHandler nullHandler;
  private final KfSessionHandler kfSessionHandler;
  private final MenuHandler menuHandler;
  private final MsgHandler msgHandler;
  private final ScanHandler scanHandler;
  private final UnsubscribeHandler unsubscribeHandler;
  private final SubscribeHandler subscribeHandler;
  @Bean
  public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
    final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
    // 记录所有事件的日志
    newRouter.rule().async(false).handler(this.logHandler).next();
    // 接收客服会话管理事件
    newRouter.rule().async(false).msgType(EVENT).event(KF_CREATE_SESSION).handler(this.kfSessionHandler).end();
    // 自定义菜单事件
    newRouter.rule().async(false).msgType(EVENT).event(EventType.CLICK).handler(this.menuHandler).end();
    // 关注事件
    newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end();
    // 取消关注事件
    newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end();
    //扫描带参二维码事件
    newRouter.rule().async(false).msgType(EVENT).event(SCAN).handler(this.scanHandler).end();
    //其他事件
    newRouter.rule().async(false).msgType(EVENT).handler(this.nullHandler).end();
    // 默认
    newRouter.rule().async(false).handler(this.msgHandler).end();
    return newRouter;
  }
}
